home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / doc.c < prev    next >
C/C++ Source or Header  |  1992-05-23  |  12KB  |  436 lines

  1. /* Record indices of function doc strings stored in a file.
  2.    Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "lisp.h"
  23. #include "buffer.h"
  24. #include "paths.h"
  25.  
  26. #include <sys/types.h>
  27. #include <sys/file.h>    /* Must be after sys/types.h for USG and BSD4_1*/
  28.  
  29. #ifdef USG5
  30. #include <fcntl.h>
  31. #endif
  32.  
  33. #ifndef O_RDONLY
  34. #define O_RDONLY 0
  35. #endif
  36.  
  37. Lisp_Object Vdoc_file_name;
  38.  
  39. Lisp_Object
  40. get_doc_string (filepos)
  41.      long filepos;
  42. {
  43.   char buf[512 * 32 + 1];
  44.   register int fd;
  45.   register char *name;
  46.   register char *p, *p1;
  47.   register int count;
  48.   extern char *index ();
  49.  
  50.   if (XTYPE (Vexec_directory) != Lisp_String
  51.       || XTYPE (Vdoc_file_name) != Lisp_String)
  52.     return Qnil;
  53.  
  54.   name = (char *) alloca (XSTRING (Vexec_directory)->size
  55.               + XSTRING (Vdoc_file_name)->size + 8);
  56.   strcpy (name, XSTRING (Vexec_directory)->data);
  57.   strcat (name, XSTRING (Vdoc_file_name)->data);
  58. #ifdef VMS
  59. #ifndef VMS4_4
  60.   /* For VMS versions with limited file name syntax,
  61.      convert the name to something VMS will allow.  */
  62.   p = name;
  63.   while (*p)
  64.     {
  65.       if (*p == '-')
  66.     *p = '_';
  67.       p++;
  68.     }
  69. #endif /* not VMS4_4 */
  70. #ifdef VMS4_4
  71.   strcpy (name, sys_translate_unix (name));
  72. #endif /* VMS4_4 */
  73. #endif /* VMS */
  74.  
  75.   fd = open (name, O_RDONLY, 0);
  76.   if (fd < 0)
  77.     error ("Cannot open doc string file \"%s\"", name);
  78.   if (0 > lseek (fd, filepos, 0))
  79.     {
  80.       close (fd);
  81.       error ("Position %ld out of range in doc string file \"%s\"",
  82.          filepos, name);
  83.     }
  84.   p = buf;
  85.   while (p != buf + sizeof buf - 1)
  86.     {
  87.       count = read (fd, p, 512);
  88.       p[count] = 0;
  89.       if (!count)
  90.     break;
  91.       p1 = index (p, '\037');
  92.       if (p1)
  93.     {
  94.       *p1 = 0;
  95.       p = p1;
  96.       break;
  97.     }
  98.       p += count;
  99.     }
  100.   close (fd);
  101.   return make_string (buf, p - buf);
  102. }
  103.  
  104. DEFUN ("documentation", Fdocumentation, Sdocumentation, 1, 1, 0,
  105.   "Return the documentation string of FUNCTION.")
  106.   (fun1)
  107.      Lisp_Object fun1;
  108. {
  109.   Lisp_Object fun;
  110.   Lisp_Object funcar;
  111.   Lisp_Object tem;
  112.  
  113.   fun = fun1;
  114.   while (XTYPE (fun) == Lisp_Symbol)
  115.     fun = Fsymbol_function (fun);
  116.   if (XTYPE (fun) == Lisp_Subr)
  117.     {
  118.       if (XSUBR (fun)->doc == 0) return Qnil;
  119.       if ((int) XSUBR (fun)->doc >= 0)
  120.     return Fsubstitute_command_keys (build_string (XSUBR (fun)->doc));
  121.       return Fsubstitute_command_keys (get_doc_string (- (int) XSUBR (fun)->doc));
  122.     }
  123.   if (XTYPE (fun) == Lisp_Vector)
  124.     return build_string ("Prefix command (definition is a Lisp vector of subcommands).");
  125.   if (XTYPE (fun) == Lisp_String)
  126.     return build_string ("Keyboard macro.");
  127.   if (!CONSP (fun))
  128.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  129.   funcar = Fcar (fun);
  130.   if (XTYPE (funcar) != Lisp_Symbol)
  131.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  132.   if (XSYMBOL (funcar) == XSYMBOL (Qkeymap))
  133.     return build_string ("Prefix command (definition is a list whose cdr is an alist of subcommands.)");
  134.   if (XSYMBOL (funcar) == XSYMBOL (Qlambda)
  135.       || XSYMBOL (funcar) == XSYMBOL (Qautoload))
  136.     {
  137.       tem = Fcar (Fcdr (Fcdr (fun)));
  138.       if (XTYPE (tem) == Lisp_String)
  139.     return Fsubstitute_command_keys (tem);
  140.       if (XTYPE (tem) == Lisp_Int && XINT (tem) >= 0)
  141.     return Fsubstitute_command_keys (get_doc_string (XFASTINT (tem)));
  142.       return Qnil;
  143.     }
  144.   if (XSYMBOL (funcar) == XSYMBOL (Qmocklisp))
  145.     return Qnil;
  146.   if (XSYMBOL (funcar) == XSYMBOL (Qmacro))
  147.     return Fdocumentation (Fcdr (fun));
  148.   else
  149.     return Fsignal (Qinvalid_function, Fcons (fun, Qnil));
  150. }
  151.  
  152. DEFUN ("documentation-property", Fdocumentation_property, 
  153.        Sdocumentation_property, 2, 2, 0,
  154.   "Return the documentation string that is SYMBOL's PROP property.\n\
  155. This differs from using `get' only in that it can refer to strings\n\
  156. stored in the etc/DOC file.")
  157.   (sym, prop)
  158.      Lisp_Object sym, prop;
  159. {
  160.   register Lisp_Object tem;
  161.  
  162.   tem = Fget (sym, prop);
  163.   if (XTYPE (tem) == Lisp_Int)
  164.     tem = get_doc_string (XINT (tem) > 0 ? XINT (tem) : - XINT (tem));
  165.   return Fsubstitute_command_keys (tem);
  166. }
  167.  
  168. DEFUN ("Snarf-documentation", Fsnarf_documentation, Ssnarf_documentation,
  169.   1, 1, 0,
  170.   "Used during Emacs initialization, before dumping runnable Emacs,\n\
  171. to find pointers to doc strings stored in etc/DOC... and\n\
  172. record them in function definitions.\n\
  173. One arg, FILENAME, a string which does not include a directory.\n\
  174. The file is found in ../etc now; found in the exec-directory\n\
  175. when doc strings are referred to later in the dumped Emacs.")
  176.   (filename)
  177.      Lisp_Object filename;
  178. {
  179.   int fd;
  180.   char buf[1024 + 1];
  181.   register int filled;
  182.   register int pos;
  183.   register char *p, *end;
  184.   Lisp_Object sym, fun, tem;
  185.   char *name;
  186.   extern char *index ();
  187.  
  188.   CHECK_STRING (filename, 0);
  189.  
  190. #ifndef CANNOT_DUMP
  191.   name = (char *) alloca (XSTRING (filename)->size + 8);
  192.   strcpy (name, RELPATH_DOC);
  193. #else /* CANNOT_DUMP */
  194.   CHECK_STRING (Vexec_directory, 0);
  195.   name = (char *) alloca (XSTRING (filename)->size +
  196.               XSTRING (Vexec_directory)->size + 1);
  197.   strcpy (name, XSTRING (Vexec_directory)->data);
  198. #endif /* CANNOT_DUMP */
  199.   strcat (name, XSTRING (filename)->data);     /*** Add this line ***/
  200. #ifdef VMS
  201. #ifndef VMS4_4
  202.   /* For VMS versions with limited file name syntax,
  203.      convert the name to something VMS will allow.  */
  204.   p = name;
  205.   while (*p)
  206.     {
  207.       if (*p == '-')
  208.     *p = '_';
  209.       p++;
  210.     }
  211. #endif /* not VMS4_4 */
  212. #ifdef VMS4_4
  213.   strcpy (name, sys_translate_unix (name));
  214. #endif /* VMS4_4 */
  215. #endif /* VMS */
  216.  
  217.   fd = open (name, O_RDONLY, 0);
  218.   if (fd < 0)
  219.     report_file_error ("Opening doc string file",
  220.                Fcons (build_string (name), Qnil));
  221.   Vdoc_file_name = filename;
  222.   filled = 0;
  223.   pos = 0;
  224.   while (1)
  225.     {
  226.       if (filled < 512)
  227.     filled += read (fd, &buf[filled], sizeof buf - 1 - filled);
  228.       if (!filled)
  229.     break;
  230.  
  231.       buf[filled] = 0;
  232.       p = buf;
  233.       end = buf + (filled < 512 ? filled : filled - 128);
  234.       while (p != end && *p != '\037') p++;
  235.       /* p points to ^_Ffunctionname\n or ^_Vvarname\n.  */
  236.       if (p != end)
  237.     {
  238.       end = index (p, '\n');
  239.       sym = oblookup (Vobarray, p + 2, end - p - 2);
  240.       if (XTYPE (sym) == Lisp_Symbol)
  241.         {
  242.           if (p[1] == 'V')
  243.         {
  244.           /* Install file-position as variable-documentation property
  245.              and make it negative for a user-variable
  246.              (doc starts with a `*').  */
  247.           Fput (sym, Qvariable_documentation,
  248.             make_number ((pos + end + 1 - buf)
  249.                      * (end[1] == '*' ? -1 : 1)));
  250.         }
  251.           else if (p[1] == 'F')
  252.         {
  253.           fun = XSYMBOL (sym)->function;
  254.           if (XTYPE (fun) == Lisp_Subr)
  255.             XSUBR (fun)->doc = (char *) - (pos + end + 1 - buf);
  256.           else if (CONSP (fun))
  257.             {
  258.               tem = XCONS (fun)->car;
  259.               if (EQ (tem, Qlambda) || EQ (tem, Qautoload))
  260.             {
  261.               tem = Fcdr (Fcdr (fun));
  262.               if (CONSP (tem) &&
  263.                   XTYPE (XCONS (tem)->car) == Lisp_Int)
  264.                 XFASTINT (XCONS (tem)->car) = (pos + end + 1 - buf);
  265.             }
  266.             }
  267.         }
  268.           else error ("DOC file invalid at position %d", pos);
  269.         }
  270.     }
  271.       pos += end - buf;
  272.       filled -= end - buf;
  273.       bcopy (end, buf, filled);
  274.     }
  275.   close (fd);
  276.   return Qnil;
  277. }
  278.  
  279. DEFUN ("substitute-command-keys", Fsubstitute_command_keys,
  280.   Ssubstitute_command_keys, 1, 1, 0,
  281.   "Return the STRING with substrings of the form \\=\\[COMMAND]\n\
  282. replaced by either:  a keystroke sequence that will invoke COMMAND,\n\
  283. or \"M-x COMMAND\" if COMMAND is not on any keys.\n\
  284. Substrings of the form \\=\\{MAPVAR} are replaced by summaries\n\
  285. \(made by describe-bindings) of the value of MAPVAR, taken as a keymap.\n\
  286. Substrings of the form \\=\\<MAPVAR> specify to use the value of MAPVAR\n\
  287. as the keymap for future \\=\\[COMMAND] substrings.\n\
  288. \\=\\= quotes the following character and is discarded;\n\
  289. thus, \\=\\=\\=\\= puts \\=\\= into the output, and \\=\\=\\=\\[ puts \\=\\[ into the output.")
  290.   (str)
  291.      Lisp_Object str;
  292. {
  293.   unsigned char *buf;
  294.   int changed = 0;
  295.   register unsigned char *strp;
  296.   register unsigned char *bufp;
  297.   int idx;
  298.   int bsize;
  299.   unsigned char *new;
  300.   register Lisp_Object tem;
  301.   Lisp_Object keymap;
  302.   unsigned char *start;
  303.   int length;
  304.   struct gcpro gcpro1; 
  305.  
  306.   if (NULL (str))
  307.     return Qnil;
  308.  
  309.   CHECK_STRING (str, 0);
  310.   GCPRO1 (str);
  311.  
  312.   keymap = current_buffer->keymap;
  313.  
  314.   bsize = XSTRING (str)->size;
  315.   bufp = buf = (unsigned char *) xmalloc (bsize);
  316.  
  317.   strp = (unsigned char *) XSTRING (str)->data;
  318.   while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size)
  319.     {
  320.       if (strp[0] == '\\' && strp[1] == '=')
  321.     {
  322.       /* \= quotes the next character;
  323.          thus, to put in \[ without its special meaning, use \=\[.  */
  324.       changed = 1;
  325.       *bufp++ = strp[2];
  326.       strp += 3;
  327.     }
  328.       else if (strp[0] == '\\' && strp[1] == '[')
  329.     {
  330.       changed = 1;
  331.       strp += 2;        /* skip \[ */
  332.       start = strp;
  333.  
  334.       while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size
  335.          && *strp != ']')
  336.         strp++;
  337.       length = strp - start;
  338.       strp++;        /* skip ] */
  339.  
  340.       /* Save STRP in IDX.  */
  341.       idx = strp - (unsigned char *) XSTRING (str)->data;
  342.       tem = Fintern (make_string (start, length), Qnil);
  343.       tem = Fwhere_is_internal (tem, keymap, Qt);
  344.  
  345.       if (NULL (tem))    /* but not on any keys */
  346.         {
  347.           new = (unsigned char *) xrealloc (buf, bsize += 4);
  348.           bufp += new - buf;
  349.           buf = new;
  350.           bcopy ("M-x ", bufp, 4);
  351.           bufp += 4;
  352.           goto subst;
  353.         }
  354.       else
  355.         {            /* function is on a key */
  356.           tem = Fkey_description (tem);
  357.           goto subst_string;
  358.         }
  359.     }
  360.       /* \{foo} is replaced with a summary of the keymap (symeval foo).
  361.      \<foo> just sets the keymap used for \[cmd].  */
  362.       else if (strp[0] == '\\' && (strp[1] == '{' || strp[1] == '<'))
  363.     {
  364.       struct buffer *oldbuf;
  365.       Lisp_Object name;
  366.  
  367.       changed = 1;
  368.       strp += 2;        /* skip \{ or \< */
  369.       start = strp;
  370.  
  371.       while (strp - (unsigned char *) XSTRING (str)->data < XSTRING (str)->size
  372.          && *strp != '}' && *strp != '>')
  373.         strp++;
  374.       length = strp - start;
  375.       strp++;            /* skip } or > */
  376.  
  377.       /* Save STRP in IDX.  */
  378.       idx = strp - (unsigned char *) XSTRING (str)->data;
  379.  
  380.       oldbuf = current_buffer;
  381.       set_buffer_internal (XBUFFER (Vprin1_to_string_buffer));
  382.       name = Fintern (make_string (start, length), Qnil);
  383.       if ((tem = (Fboundp (name)), NULL (tem)) ||
  384.           (tem = (Fsymbol_value (name)), NULL (tem)) ||
  385.           (tem = (get_keymap_1 (tem, 0)), NULL (tem)))
  386.         {
  387.           name = Fsymbol_name (name);
  388.           InsStr ("\nUses keymap \"");
  389.           insert (XSTRING (name)->data, XSTRING (name)->size);
  390.           InsStr ("\", which is not currently defined.\n");
  391.           if (start[-1] == '<') keymap = Qnil;
  392.         }
  393.       else if (start[-1] == '<')
  394.         keymap = tem;
  395.       else
  396.         describe_map_tree (tem, 1, Qnil);
  397.       tem = Fbuffer_string ();
  398.       Ferase_buffer ();
  399.       set_buffer_internal (oldbuf);
  400.  
  401.     subst_string:
  402.       start = XSTRING (tem)->data;
  403.       length = XSTRING (tem)->size;
  404.     subst:
  405.       new = (unsigned char *) xrealloc (buf, bsize += length);
  406.       bufp += new - buf;
  407.       buf = new;
  408.       bcopy (start, bufp, length);
  409.       bufp += length;
  410.       /* Check STR again in case gc relocated it.  */
  411.       strp = (unsigned char *) XSTRING (str)->data + idx;
  412.     }
  413.       else            /* just copy other chars */
  414.     *bufp++ = *strp++;
  415.      }
  416.  
  417.   if (changed)            /* don't bother if nothing substituted */
  418.     tem = make_string (buf, bufp - buf);
  419.   else
  420.     tem = str;
  421.   UNGCPRO;
  422.   free (buf);
  423.   return tem;
  424. }
  425.  
  426. syms_of_doc ()
  427. {
  428.   staticpro (&Vdoc_file_name);
  429.   Vdoc_file_name = Qnil;
  430.  
  431.   defsubr (&Sdocumentation);
  432.   defsubr (&Sdocumentation_property);
  433.   defsubr (&Ssnarf_documentation);
  434.   defsubr (&Ssubstitute_command_keys);
  435. }
  436.